package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.dao.bean.ItemSet;
import com.qilu.pe.dao.bean.SickDetail;
import com.qilu.pe.ui.activity.ItemSetDetailActivity;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// 疾病详情2
public class SickDetail2Fragment extends BaseFragment {
    private RecyclerView rcv_list;

    private SickDetailItemListAdapter adapter;
    private SickDetail detail;
    private List<ItemSet> itemSetList = new ArrayList<>();

    public SickDetail2Fragment() {

    }

    public static SickDetail2Fragment newInstance(String param1, SickDetail detail) {
        SickDetail2Fragment fragment = new SickDetail2Fragment();
        Bundle args = new Bundle();
        args.putSerializable("detail", detail);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        detail = (SickDetail) getArguments().getSerializable("detail");
        if (null != detail) {
            itemSetList = detail.getProject_list();
        }
        adapter = new SickDetailItemListAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_sick_detail2, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        adapter.setDataList(itemSetList);

        setListeners();

        return v;
    }

    private void setListeners() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("type", "2");
                bundle.putString("id", itemSetList.get(position).getId());
                startActivity(ItemSetDetailActivity.class, bundle);
            }
        });
    }

    private class SickDetailItemListAdapter extends ListBaseAdapter<ItemSet> {

        public SickDetailItemListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sick_detail_item_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tv_day = holder.getView(R.id.tv_day);
            TextView tv_purpose = holder.getView(R.id.tv_purpose);

            ItemSet itemSet = mDataList.get(position);
            tv_name.setText(itemSet.getName());
            tv_price.setText("¥" + itemSet.getPrice());
            tv_day.setText(itemSet.getDay() + "个工作日");
            tv_purpose.setText("检测目的:" + itemSet.getSignificance());
        }
    }
}